package com.senlingspace.payment.lakala.service.impl;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Joiner;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.V3LamsMerchantQueryTransRequest;
import com.senlingspace.common.core.dingtalk.DingTalkService;
import com.senlingspace.common.exception.payment.LakalaException;
import com.senlingspace.payment.lakala.bean.request.LakalaFeeAmountRequest;
import com.senlingspace.payment.lakala.bean.response.LakalaFeeAmountResponse;
import com.senlingspace.payment.lakala.service.LakalaTransFeeService;
import com.senlingspace.system.domain.SlkjMerchant;
import com.senlingspace.system.mapper.SlkjMerchantMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * lakala交易手续费查询接口实现类
 *
 * <a href="https://o.lakala.com/#/home/document/detail?id=553">商户计费查询</a>
 *
 * @author lebronjames
 * @date 2024/08/09
 */
@AllArgsConstructor
@Slf4j
@Service
public class LakalaTransFeeServiceImpl implements LakalaTransFeeService {

    private final DingTalkService dingTalkService;

    private final SlkjMerchantMapper slkjMerchantMapper;


    @Override
    public LakalaFeeAmountResponse feeAmount(LakalaFeeAmountRequest request) throws LakalaException {
        log.info("lakala交易手续费查询，接口入参：{}", JSON.toJSONString(request));
        SlkjMerchant merchant = slkjMerchantMapper.selectByShopId(request.getShopId());
        if (Objects.isNull(merchant) || StringUtils.isBlank(merchant.getTermNo()) || StringUtils.isBlank(merchant.getMerchantNo())) {
            log.error("门店 [{}] 商户信息配置有误！", request.getShopId());
            String logMessage = Joiner.on("").join("门店 [", request.getShopId(), "] 商户信息配置有误，请检查 [slkj_merchant]！");
            dingTalkService.sendTextMessage(logMessage, true);
            throw new LakalaException("门店商户信息有误！");
        }

        V3LamsMerchantQueryTransRequest transRequest = new V3LamsMerchantQueryTransRequest();
        try {
            transRequest.setMerchantNo(merchant.getMerchantNo());
            transRequest.setTermNo(merchant.getTermNo());
        } catch (LakalaException e) {
            log.error("拉卡拉手续费查询失败，商户信息不存在：{}", e.getMessage());
            dingTalkService.sendTextMessage("拉卡拉手续费查询失败，商户信息不存在：" + e.getMessage() + "，对应入参：logNo=" + request.getLogNo(), true);
            return null;
        }

        transRequest.setLogNo(request.getLogNo());
        log.info("lakala交易手续费查询，请求参数：{}", JSON.toJSONString(transRequest));
        try {
            String json = LKLSDK.httpPost(transRequest);
            log.info("拉卡拉手续费查询结果：{}", json);
            if (StringUtils.isBlank(json)) {
                log.error("拉卡拉手续费查询失败，拉卡拉返回数据异常");
                dingTalkService.sendTextMessage("拉卡拉手续费查询失败，拉卡拉返回数据为空！" + "对应入参：logNo=" + request.getLogNo(), true);
                return null;
            }
            return JSON.parseObject(json, LakalaFeeAmountResponse.class);
        } catch (SDKException e) {
            log.error("拉卡拉手续费查询失败，拉卡拉系统异常：{}", e.getMessage());
            dingTalkService.sendTextMessage("拉卡拉手续费查询失败，拉卡拉系统异常：" + e.getMessage() + "，对应入参：logNo=" + request.getLogNo(), true);
            return null;
        }
    }

}
